﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using Aandelendelen.Db;
using Aandelendelen.Obs;
using Aandelendelen.Mail;

namespace Aandelendelen
{
	public partial class ActivateAccount : BaseForm
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				this.Activate();
			}
		}

		private void Activate()
		{
			try
			{
				string uid = this.ActivateStep1();

				this.ActivateStep2(uid);

				this.ActivateStep3(uid);

				this.Label1.Text = "Uw account is succesvol geactiveerd.";
				this.UserName.Text = uid;

				this.SendConfirmationMail(uid);

				this.Panel1.Visible = true;
			}
			catch (Exception e)
			{
				this.Panel1.Visible = false;

				this.Label1.Text = e.Message;
			}
		}

		private string ActivateStep1()
		{
			string hashedUid = this.Request.QueryString["uid"];

			if (hashedUid == null)
				throw new Exception("Deze pagina mag niet zomaar aangeroepen worden.");

			Query query = new Query("s_fromactivate", "hashed", hashedUid, OleDbType.Char);

			string uid = (string)query.Execute();
			if (uid == null)
				throw new Exception("Dit account bestaat niet of is reeds geactiveerd.");

			return uid;
		}

		private void ActivateStep2(string uid)
		{
			Query query = new Query("sp_activateuser", "gebruikersnaam", uid, OleDbType.Char);

			int rows = (int)query.Execute();

			if (rows == 0)
				throw new Exception("Dit account bestaat niet of is reeds geactiveerd.");
		}

		private void ActivateStep3(string uid)
		{
			Query query = new Query("sp_deleteuserfromactivate", "gebruikersnaam", uid, OleDbType.Char);

			int rows = (int)query.Execute();

			if (rows == 0)
				throw new Exception("Dit account bestaat niet of is reeds geactiveerd.");
		}

		private void SendConfirmationMail(string uid)
		{
			BulkEMail emails = new BulkEMail();

			Query query = new Query("s_nawimpersonate", "Gebruikersnaam", uid, OleDbType.Char);

			ResultSet resultSet = (ResultSet)query.Execute();

			Userinfo userInfo = new Userinfo(resultSet, false);
			if (userInfo.Referrer != 0)
			{
				Query refQuery = new Query("s_getemailfromid", "Id", userInfo.Referrer, OleDbType.Integer);
				ResultSet refResultSet = (ResultSet)refQuery.Execute();
				if (refResultSet.Count > 0)
				{
					Userinfo referrer = new Userinfo(refResultSet, true);

					MailArguments refMailArgs = MailArguments.MailArgs;
					refMailArgs.Clear();

					refMailArgs["{aanhef}"] = referrer.MailAanhef;
					refMailArgs["{aantal}"] = referrer.Referrals.Count.ToString();

					EMail refEmail = MailCache.GetMail("referralregistered");
					refEmail.Recipients = referrer.Email;

					emails.Add(refEmail);
				}
			}

			MailArguments mailArgs = MailArguments.MailArgs;
			mailArgs.Clear();

			mailArgs["{aanhef}"] = userInfo.MailAanhef;
			mailArgs["{username}"] = uid;

			EMail email = MailCache.GetMail("accountactivated");
			email.Recipients = userInfo.Email;

			emails.Add(email);

			emails.Send(true);
		}
	}
}